<template>
  <div>
    <el-dialog
      v-for ='(item, index) in db_licence'
      :key="item.typeCode"
      width="70%"
      v-dialogDrag
      :title="item.typeValue"
      :visible.sync="that[that.db_licence[index].typeCode]"
      append-to-body>
      <hr>
      <span v-if="!look_flag">
            <image-upload
              v-if="!(item.typeCode === 'lic11' || item.typeCode === 'lic12')"
              :type="item.typeCode"
              :type_code ='temp'
              :id_code = 'id'
              :license_type_id_code='licenseTypeId'
              attach-type="01"
              button-title="上传正面"
              @get_data="getUpLoadData"
              @distinguish_info='distinguishInfo'></image-upload>
            <image-upload
              v-if="!(item.typeCode === 'lic11' || item.typeCode === 'lic12')"
              :type="item.typeCode"
              :type_code ='temp'
              :id_code = 'id'
              :license_type_id_code='licenseTypeId'
              attach-type="02"
              button-title="上传反面"
              @get_data="getUpLoadData"
              @distinguish_info='distinguishInfo'></image-upload>
            <image-upload
              v-if="item.typeCode === 'lic07' || item.typeCode === 'lic01'"
              :type="item.typeCode"
              :type_code ='temp'
              :id_code = 'id'
              :license_type_id_code='licenseTypeId'
              attach-type="03"
              button-title="上传手持照"
              @get_data="getUpLoadData"></image-upload>
            <image-upload
              v-if="item.typeCode === 'lic11'"
              :type="item.typeCode"
              :type_code ='temp'
              :id_code = 'id'
              :license_type_id_code='licenseTypeId'
              attach-type="04"
              button-title="上传牵引车照"
              @get_data="getUpLoadData"></image-upload>
            <image-upload
              v-if="item.typeCode === 'lic12'"
              :type="item.typeCode"
              :type_code ='temp'
              :id_code = 'id'
              :license_type_id_code='licenseTypeId'
              attach-type="05"
              button-title="上传挂车照"
              @get_data="getUpLoadData"></image-upload>
          </span>
      <el-table :data="license_attach_list_res" v-loading="image_list_loading" border style="width: 100%;margin-top: 6px">
        <el-table-column type="index" label="序号" width="50"></el-table-column>
        <el-table-column prop="attachName" label="附件名" >
          <template slot-scope="scope">
            <span class="image-detail-name" @click="seeImage(scope.row)">{{scope.row.attachName}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="format" label="附件格式" ></el-table-column>
        <el-table-column label="文件大小" width="100px" >
          <template slot-scope="scope">
            {{scope.row.size | manSize}}
          </template>
        </el-table-column>
        <el-table-column prop="uploadTime" label="上传时间" ></el-table-column>
        <el-table-column label="照片正反面" >
          <template slot-scope="scope">
            {{scope.row.attachType | getPhotoType}}
          </template>
        </el-table-column>
        <el-table-column  prop="attachName" label="附件缩略图">
          <template slot-scope="scope">

            <el-tooltip placement="left">
              <div slot="content" style="max-width: 600px; max-height: 600px; overflow: scroll;">
                <img class="img-preview" :src="scope.row.lookUrl" style="max-width: 1000px;height: auto;"/>
              </div>
              <img :src="scope.row.lookUrl" style="height: 30px;" />
            </el-tooltip>

          </template>
        </el-table-column>
        <el-table-column  class="img-preview" fixed="right" label="操作" width="180px" >
          <template slot-scope="scope">
            <a :href="scope.row.downloadUrl" v-show="scope.row.downloadUrl" class="downloadImage">
              下载证照 <i class="el-icon-download"></i>
            </a>
            <el-button type="text" v-if="!look_flag" @click="deleteImage(scope.$index)" size="small">
              删除 <i class="el-icon-close"></i>
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="uploadTips">{{tools.uploadTips()}}</div>
      <el-dialog
        width="60%"
        v-dialogDrag
        :title="item.typeValue"
        :visible.sync="ImageDetailDialog"
        append-to-body>
        <hr>
        <image_loading :src="ImageDetailDialog_url"></image_loading>
      </el-dialog>
    </el-dialog>
  </div>
</template>

<script>
  /**
   *  ---- 参数说明 --------
   * db_licence                   证照种类的基础数据
   * .
   * license_attach_list          证照下面附件的具体信息
   * .
   * look_flag                    模式： （新增和编辑） 与 （查看）
   * .
   * opt_code                     要查看那种附件的类型  例如： 'lic01', 'lic02'
   * .
   * license_attach_list_res      最终要抛出的数据
   * .
   * now_time                     每次父组件点击，将其设置为当前时间，用于触发watch事件以刷新弹框数据
   */
  import imageUpload from '@/components/imageUpload/index'
  import image_loading from '@/components/image_loading'
  import * as lspMgtAPI from '@/api/lspMgt'
  export default {
    name: 'lic-upload',
    components: {
      imageUpload,
      image_loading
    },
    props: {
      id: {
        type: Number,
        default: function() {
          return null
        }
      },
      type: {
        type: String,
        default: function() {
          return ''
        }
      },
      db_licence: {
        type: Array,
        default: function() {
          return []
        }
      },
      license_attach_list: {
        type: Array,
        default: function() {
          return []
        }
      },
      look_flag: {
        type: Boolean,
        default: function() {
          return false
        }
      },
      opt_code: {
        type: String,
        default: function() {
          return ''
        }
      },
      now_time: {
        type: String,
        default: function() {
          return ''
        }
      }
    },
    watch: {
      // 监听时间， 实现每次进入页面都执行初始化函数
      now_time(oldValue, newValue) {
        this.initDialog()
        this[this.opt_code] = true
      }
    },
    beforeCreate() {
    },
    created() {
      this.temp = this.type
      this.license_attach_list_res = []
    },
    computed: {
    },
    data() {
      return {
        that: this,
        lic00: false,
        lic01: false,
        lic02: false,
        lic03: false,
        lic04: false,
        lic05: false,
        lic06: false,
        lic07: false,
        lic08: false,
        lic09: false,
        lic10: false,
        lic11: false,
        lic12: false,
        lic13: false,
        image_list_loading: false,
        ImageDetailDialog: false,
        ImageDetailDialog_url: '',
        license_attach_list_res: [],
        currentLicData: {}, // 那种证照
        curLicDetail: {}, // 证照的具体内容 正反面
        licenseTypeId: null,
        temp: ''
      }
    },
    methods: {
      // 获取证件的licenseId
      getLicenseId() {
        const a = this.opt_code
        const id_t = a.replace('lic', '')
        const reg = /^0/g
        if (this.opt_code && reg.test(id_t)) {
          this.licenseTypeId = parseInt(a.replace('lic0', ''))
        } else {
          this.licenseTypeId = parseInt(a.replace('lic', ''))
        }
      },
      updateImageInfo(obj) {
        const that = this
        // 拼接参数
        const t = {
          ...obj,
          licenseTypeId: that.licenseTypeId,
          belongedId: that.id,
          belongedType: that.type
        }
        lspMgtAPI.createAttach(t).then(res => {
          // 在数据库保存数据后，将返回的id帮到对应的数据上， 方便后面删除
          this.license_attach_list_res.find((i, j) => {
            if (obj.attachType === i.attachType) {
              this.license_attach_list_res[j].attachId = res.data[0].attachId
            }
          })
          console.log('替换that.license_attach_list_res', this.license_attach_list_res)
        }).catch(() => {
          //
        })
      },
      // 初始化弹框信息，将后台返回的key数组，转化为文件的具体信息
      initDialog() {
        const that = this
        // 转化操作证照
        this.getLicenseId()
        // 获取附件的具体信息
        that.image_list_loading = true
        lspMgtAPI.queryAttachList({
          licenseTypeId: that.licenseTypeId,
          belongedId: that.id,
          belongedType: that.type
        }).then(res => {
          that.license_attach_list_res = res.data
          that.image_list_loading = false
        }).catch(() => {
          that.image_list_loading = false
          //
        })
      },
      // 图片上传成功后
      getUpLoadData(d) {
        const that = this
        // 判断当前上传的图片是否在表格中
        // 如果在， 就替代原来的值 ， 如果不在就添加进表格
        // 默认不在
        let e = true
        const licList = d.data.licenseAttachList
        console.log('licList', licList)
        console.log('that.license_attach_list_res', that.license_attach_list_res)
        that.license_attach_list_res.map(i => {
          // 已上传
          if (licList.attachType === i.attachType) {
            if (licList.attachKey === i.attachKey) {
              e = false
              for (const k in licList) {
                i[k] = licList[k]
              }
            }
          }
        })
        console.log('更换后that.license_attach_list_res', that.license_attach_list_res)
        // 未上传
        if (e) {
          that.license_attach_list_res.push(d.data.licenseAttachList)
        }
        // 上面是将图片保在table的中， 下面将数据放到返回给后端的数据结构中
        that.updateImageInfo(d.data.licenseAttachList)
        // 向父级抛出更新后的数据
        that.$emit('data', that.license_attach_list)
      },
      // 证照识别
      distinguishInfo(d) {
        const that = this
        // 驾照
        if (d.licenseTypeId === 2) {
          this.license_attach_list.map((i, j) => {
            if (d.licenseTypeId === parseInt(i.licenseTypeId)) {
              that.license_attach_list[j].licenseId = d.data.number
              that.license_attach_list[j].gmtReg = that.tools.formatLicTime(d.data.initialdate)
              that.license_attach_list[j].gmtOverdue = that.tools.formatLicTime(d.data.enddate)
            }
          })
        } else if (d.licenseTypeId === 1 || d.licenseTypeId === 7) {
          // 身份证 正面
          if (d.attachType === '01') {
            this.license_attach_list.map((i, j) => {
              if (d.licenseTypeId === parseInt(i.licenseTypeId)) {
                that.license_attach_list[j].licenseId = d.data.number
              }
            })
            // 身份证 反面
          } else if (d.attachType === '02') {
            this.license_attach_list.map((i, j) => {
              if (d.licenseTypeId === parseInt(i.licenseTypeId)) {
                that.license_attach_list[j].gmtReg = that.tools.formatLicTime(d.data.start_date)
                that.license_attach_list[j].gmtOverdue = that.tools.formatLicTime(d.data.end_date)
              }
            })
          }
        } else if (d.licenseTypeId === 10) {
          // 行驶证
          this.license_attach_list.map((i, j) => {
            if (d.licenseTypeId === parseInt(i.licenseTypeId)) {
              that.license_attach_list[j].licenseId = d.data.vin
              that.license_attach_list[j].gmtReg = that.tools.formatLicTime(d.data.issuedate)
              that.license_attach_list[j].gmtOverdue = that.tools.formatLicTime(d.data.gmtOverdue)
            }
          })
        }
        // 识别成功时。向父级抛出licenseTypeId，在父级将附件 长期有效 清除掉
        that.$emit('clear_perm', d.licenseTypeId)
        // 向父级抛出更新后的数据
        that.$emit('data', that.license_attach_list)
      },
      // 查看图片详情
      seeImage(row) {
        this.ImageDetailDialog_url = row.lookUrl
        this.ImageDetailDialog = true
      },
      // 删除图片
      deleteImage(index) {
        const that = this
        //
        // 先删除table中的数据
        const curr_t = that.license_attach_list_res[index]
        that.license_attach_list_res.splice(index, 1)
        console.log('curr_t', curr_t)
        // 再去请求接口，在后端数据库软删附件数据
        lspMgtAPI.deleteAttach({
          belongedType: that.type,
          attachId: curr_t.attachId
        }).then(res => {
          //
        }).catch(() => {
          //
        })
      }
    }
  }
</script>
<style>

  /*.img-preview:hover{*/
    /*transform: scale(1);*/
    /*!*offsef:90;*/
    /*transition:none;*!*/
    /*max-width: 80%;*/
    /*max-height: 80%;*/
  /*}*/
</style>
